package com.example.neusoft.yuejian.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.example.neusoft.yuejian.Base.BaseActivity;
import com.example.neusoft.yuejian.Base.BaseListener;
import com.example.neusoft.yuejian.Base.BaseModel;
import com.example.neusoft.yuejian.Bean.LoginBean;
import com.example.neusoft.yuejian.R;

import retrofit2.Call;

public class Login extends BaseActivity implements View.OnClickListener {

    private Button login;
    private EditText user, pasword;
    private CheckBox cb;
    private TextView reg;
    private String username, password;
    private Context context;
    private SharedPreferences sp;
    private LoginBean loginBean;
    String hy;
    String mm;




    @Override
    public int getLayoutFile() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {

        user = (EditText) findViewById(R.id.user);
        pasword = (EditText) findViewById(R.id.pasword);
        login = (Button) findViewById(R.id.login);
        cb = (CheckBox) findViewById(R.id.cb);
        reg = (TextView) findViewById(R.id.reg);

        Intent intent = getIntent();
        String recvce = intent.getStringExtra("message");
        Toast.makeText(Login.this, recvce, Toast.LENGTH_SHORT).show();
        user.setText(recvce);


    }


    @Override
   public void initEvent() {
        login.setOnClickListener(this);
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Login.this, Regist.class);
                startActivity(intent);
            }
        });
    }


    @Override
   public void initData() {

    }

    public void saveUser() {
        SharedPreferences sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", user.getText().toString());
        editor.putString("password", pasword.getText().toString());
        editor.putBoolean("remember", cb.isChecked());
        editor.commit();
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.login:
                if (user.getText().toString().trim().equals("") ||
                        pasword.getText().toString().equals("")) {
                    showToast("不能为空");
                } else {
                    hy = user.getText().toString();
                    mm = pasword.getText().toString();
                    login(hy, mm);
                    saveUser();
                    break;
                }
        }
    }

    public void login(String u, String p) {
        BaseModel<LoginBean> model = new BaseModel<>();
        Call<LoginBean> call = model.service.getlogin(u, p);
        model.callEnqueue(call, listener);
    }
    BaseListener<LoginBean> listener = new BaseListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if (loginBean.getUserid()==0){
                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Login.this, ExerciseActivity.class);
                sp = getSharedPreferences("User", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                edit.putInt("id",loginBean.getUserid());
                edit.commit();
               startActivity(intent);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
}



